﻿using System.Collections.Generic;
using CfgTable;
using IQIGame.Onigao.Game;

namespace IQIGame.Onigao.GamePlay.Editors
{
    public class UnitEditorFactory
    {
        // 编辑器创建Unit
        public static UnitView CreateEditorViewHero(int nHeroID, TbCfgHero rTable)
        {
            var rConfig = rTable.Get(nHeroID);
            var rSkillList = new List<int>(rConfig.DefaultSkill);

            var rUnit = CreateEditorUnitView(0, nHeroID, EUnitType.Hero, rSkillList);
            rUnit.unitType = EUnitType.Hero;
            rUnit.configID = nHeroID;
            return rUnit;
        }

        public static UnitView CreateEditorViewMonster(int nMonsterID, TbCfgMonster rTable)
        {
            var rConfig = rTable.Get(nMonsterID);
            var rUnit = CreateEditorUnitView(0, nMonsterID, EUnitType.Monster, rConfig.Skill);
            rUnit.unitType = EUnitType.Monster;
            rUnit.configID = nMonsterID;

            return rUnit;
        }

        private static UnitView CreateEditorUnitView(int guid, int nConfigID, EUnitType rType, IEnumerable<int> rSkillList)
        {
            var rUnit = new UnitView()
            {
                configID = nConfigID,
                GUID = guid,
                unitType = rType,
            };
            var rUnitProperty = new UnitViewProperty();
            if (rUnit.unitType == EUnitType.Hero)
            {
                var rConfig = TableCenter.hero.Get(rUnit.configID);
                rUnitProperty.radius = rConfig.Radius / 100f;
                int skinCid = rConfig.HeroSkinIds[0];
                var rSkinConfig = TableCenter.heroSkin.Get(skinCid);
                rUnitProperty.roleName = rSkinConfig.BattleEntityAsset;
                rUnitProperty.resPath = rSkinConfig.BattleEntityAsset_fullPath;
                rUnitProperty.entityType = (int)EUnitType.Hero;
                rUnitProperty.battleHeadIconPath = rSkinConfig.BattleHeadIcon_fullPath;
                rUnitProperty.dialog = rConfig.Diglog;
                rUnitProperty.name = rConfig.Name;
                rUnitProperty.widgetPath = PathConstant.GetHeroWidgetPath(rUnitProperty.roleName, rSkinConfig.WidgetEntityAsset);
            }
            else
            {
                var rConfig = TableCenter.monster.Get(rUnit.configID);
                rUnitProperty.roleName = rConfig.EntityAsset;
                rUnitProperty.resPath = rConfig.EntityAsset_fullPath;
                rUnitProperty.entityType = (int)EUnitType.Monster;
                rUnitProperty.radius = rConfig.Radius / 100f;
                rUnitProperty.weaknessTypeList = rConfig.WeakType;
                rUnitProperty.battleHeadIconPath = rConfig.BattleHeadIcon_fullPath;
                rUnitProperty.dialog = rConfig.Diglog;
                rUnitProperty.name = rConfig.Name;
            }

            rUnit.property = rUnitProperty;

            rUnit.AddComponent<AttributeViewComponent>(ETComponentType.AttributeViewComponent);
            rUnit.AddComponent<UnitViewComponent>(ETComponentType.UnitViewComponent);
            rUnit.AddComponent<BattleAnimationComponent>(ETComponentType.BattleAnimationComponent);
            rUnit.AddComponent<BattleMoveComponent>(ETComponentType.BattleMoveComponent);
            rUnit.AddComponent<EffectComponent>(ETComponentType.EffectComponent);
            var rSkill = rUnit.AddComponent<SkillViewComponent>(ETComponentType.SkillViewComponent);
            rSkill.Initialize(rSkillList);
            rUnit.AddComponent<BattleViewBindPointComponent>(ETComponentType.BattleViewBindPointComponent);
            rUnit.AddComponent<CustomPropertyViewComponent>(ETComponentType.CustomPropertyViewComponent);
            rUnit.AddComponent<BuffViewComponent>(ETComponentType.BuffViewComponent);
            rUnit.AddComponent<BattleTimelineComponent>(ETComponentType.BattleTimelineComponent);
            rUnit.AddComponent<BulletComponent>(ETComponentType.BulletComponent);
            rUnit.AddComponent<CollisionViewComponent>(ETComponentType.CollisionViewComponent);

            return rUnit;
        }
    }
}
